package com.example.webdemo.dao.Impl;

import com.example.webdemo.bean.orderitem;
import com.example.webdemo.dao.OrderItemDao;
import jakarta.validation.constraints.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.sql.*;
import java.util.List;

@Repository
public class OrderItemDaoImpl implements OrderItemDao {

    private final JdbcTemplate jdbcTemplate;

    @Autowired
    public OrderItemDaoImpl(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = new JdbcTemplate();
    }

    @Override
    public int createOrderItem(orderitem orderItem) {
        String sql = "INSERT INTO orderitem(order_id, prod_id, quantity, unit_price, " +
                "item_amount, item_remark) VALUES(?, ?, ?, ?, ?, ?)";

        return jdbcTemplate.update(sql,
                orderItem.getItem_id(),
                orderItem.getProd_id(),
                orderItem.getQuantity(),
                orderItem.getUnit_price(),
                orderItem.getItem_amount(),
                orderItem.getItem_remark());
    }

    @Override
    public int createOrderItems(List<orderitem> orderItems) {
        String sql = "INSERT INTO orderitem(order_id, prod_id, quantity, unit_price, " +
                "item_amount, item_remark) VALUES(?, ?, ?, ?, ?, ?)";

        return jdbcTemplate.batchUpdate(sql, new BatchPreparedStatementSetter() {
            @Override
            public void setValues(@NotNull PreparedStatement ps, int i) throws SQLException {
                orderitem item = orderItems.get(i);
                ps.setInt(1, item.getOrder_id());
                ps.setInt(2, item.getProd_id());
                ps.setInt(3, item.getQuantity());
                ps.setDouble(4, item.getUnit_price());
                ps.setDouble(5, item.getItem_amount());
                ps.setString(6, item.getItem_remark());
            }

            @Override
            public int getBatchSize() {
                return orderItems.size();
            }
        }).length;
    }

    @Override
    public orderitem getOrderItemById(int id) {
        String sql = "SELECT * FROM orderitem WHERE item_id = ?";
        return jdbcTemplate.queryForObject(sql, new OrderItemRowMapper(), id);
    }

    @Override
    public List<orderitem> getItemsByOrderId(int orderId) {
        String sql = "SELECT * FROM orderitem WHERE order_id = ?";
        return jdbcTemplate.query(sql, new OrderItemRowMapper(), orderId);
    }

    @Override
    public int updateOrderItem(orderitem orderItem) {
        String sql = "UPDATE orderitem SET prod_id = ?, quantity = ?, " +
                "unit_price = ?, item_amount = ?, item_remark = ? " +
                "WHERE item_id = ?";

        return jdbcTemplate.update(sql,
                orderItem.getProd_id(),
                orderItem.getQuantity(),
                orderItem.getUnit_price(),
                orderItem.getItem_amount(),
                orderItem.getItem_remark(),
                orderItem.getItem_id());
    }

    @Override
    public int deleteOrderItem(int id) {
        String sql = "DELETE FROM orderitem WHERE item_id = ?";
        return jdbcTemplate.update(sql, id);
    }

    @Override
    public int deleteItemsByOrderId(int orderId) {
        String sql = "DELETE FROM orderitem WHERE order_id = ?";
        return jdbcTemplate.update(sql, orderId);
    }

    private static class OrderItemRowMapper implements RowMapper<orderitem> {
        @Override
        public orderitem mapRow(ResultSet rs, int rowNum) throws SQLException {
            orderitem item = new orderitem();
            item.setItem_id(rs.getInt("item_id"));
            item.setOrder_id(rs.getInt("order_id"));
            item.setProd_id(rs.getInt("prod_id"));
            item.setQuantity(rs.getInt("quantity"));
            item.setUnit_price(rs.getDouble("unit_price"));
            item.setItem_amount(rs.getDouble("item_amount"));
            item.setItem_remark(rs.getString("item_remark"));
            return item;
        }
    }
}